<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 *
 * 创建时间：2021-11-02 10:55:12
 *
 * 项目：/levfm  -  $  - Form_levfm_fieldsHelper.php
 *
 * 作者：{{AUTO GENERATE}}
 */

//此文件使用程序自动生成，下次生成时【不会】覆盖。
//在这里可以补充、完善你的程序逻辑

namespace modules\levfm\table\form\Form_levfm_fields;

use Lev;
use lev\base\Viewv;
use lev\widgets\inputs\inputsWidget;
use modules\levfm\table\form\Form_levfm_fields\_gen\BaseForm_levfm_fields;
use modules\levfm\table\levfm_forms\levfmFormsModelHelper;
use modules\levfm\table\LevfmFields;
use modules\levfm\table\LevfmForms;
use modules\levfm\widgets\inputs\inputsFormWidget;

!defined('INLEV') && exit('Access Denied LEV');

class BaseForm_levfm_fieldsMiddle extends BaseForm_levfm_fields
{

    public static $tableName = '{{%levfm_fields}}';

    public static $opInfo = null;

    public static function saveForm($formPre = 'datax', $route = '', $form = null, $upData = null, $upDataForce = null, $opInfo = null)
    {

        $opInfo = self::setopInfo();
        $upData = Lev::stripTags(Lev::POSTv($formPre));
        $upData+= $opInfo;

        $saveRs = parent::saveForm($formPre, $route, $form, $upData, $upDataForce, $opInfo); // TODO: Change the autogenerated stub

        empty($upData['inputname']) && !empty($saveRs['opid']) &&
        static::update(['inputname'=>LevfmForms::tableFieldname($saveRs['opid'])], ['id'=>$saveRs['opid']]);

        Lev::GPv('quick') && isset($saveRs['tourl']) && $saveRs['tourl'] = 0;
        return $saveRs;
    }

    public static function tabInputs() {
        $inputs = static::inputs();
        foreach ($inputs as $k => $v) {
            if (strpos($v['inputname'], 'settings__set_') === 0) {
                unset($inputs[$k]);
                $setInputs[$v['inputname']] = $v;
            }else if (!in_array($v['inputname'], ['formid', 'classify', 'title', 'inputtype', 'placeholder', 'inputvalue', 'displayorder'])) {
                unset($inputs[$k]);
            }
        }
        return [$inputs, $setInputs];
    }

    /**
     * 额外的表单字段，不会存入数据库
     * @return array
     */
    public static function extInputs()
    {
        return parent::extInputs(); // TODO: Change the autogenerated stub
    }

    public static function headerHtm()
    {
        //return parent::headerHtm(); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义headerHtm：文件位置：'.__DIR__ . '/Form_levfm_fields.php'.'</tips>';
    }

    public static function footerHtm()
    {
        //return parent::footerHtm(); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义footerHtm</tips>';
        return Viewv::renderPartial('admin-fields/myself_form/footerHtm.php', [

        ]);
    }

    public static function footerFormInnerHtm()
    {
        return parent::footerFormInnerHtm(); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义footerFormInnerHtm</tips>';
    }

    public static function copyOne($id = null)
    {
        $rs = parent::copyOne($id); // TODO: Change the autogenerated stub
        if (!empty($rs['inid'])) {
            static::update(['inputname'=>LevfmForms::tableFieldname($rs['inid'])], ['id'=>$rs['inid']]);
        }
        return $rs;
    }

}


class Form_levfm_fields extends BaseForm_levfm_fieldsMiddle {

    public static $tempData = [
        'formInfo' => null,
    ];

    public static function setTempDataFormInfo() {
        if (static::$tempData['formInfo'] === null) {
            if ($opInfo = static::setopInfo()) {
                $formid = $opInfo['formid'];
            }else {
                $formid = Lev::GPv('formid');
            }
            $formid >0 &&
            static::$tempData['formInfo'] = levfmFormsModelHelper::findOne(['id' => $formid]);
        }
        return static::$tempData['formInfo'];
    }

    public static function setinputtype() {
        $types = inputsFormWidget::inputtype();
        return $types;
    }

    public static function setformid() {
        $arr = [];
        ($v = static::setTempDataFormInfo()) &&
        $arr[$v['id']] = $v['id'].'@'.$v['name'];

        $forms = levfmFormsModelHelper::findAllField('id,name', "1 LIMIT 200");
        if ($forms) {
            foreach ($forms as $v) {
                $arr[$v['id']] = $v['id'].'@'.$v['name'];
            }
        }
        return $arr;
    }
    public static function setclassify() {
        $arr = [];
        if ($formInfo = static::setTempDataFormInfo()) {
            if ($_fieldType = LevfmForms::getSettingsClassify($formInfo['settings'])) {
                foreach ($_fieldType as $v) {
                    $arr[$v['id']] = $v['id'].'@'.$v['name'];
                }
            }
            $arr += LevfmForms::getMyselfClassify($formInfo['id']);
        }
        empty($arr[0]) && $arr[0] = '0@未分类';
        return $arr;
    }

    public static function setsettings__tmp_field() {
        return LevfmFields::settings__tmp_field();
    }

    public static function allColumns()
    {
        return parent::allColumns(); // TODO: Change the autogenerated stub
    }

    public static function safeColumnsInpus($columns)
    {
        return parent::safeColumnsInpus($columns); // TODO: Change the autogenerated stub
    }

    public static function setFormSettings($settings, $result = [])
    {
        return parent::setFormSettings($settings, $result); // TODO: Change the autogenerated stub
    }

    public static function saveInputs($datas, $where = [], $inId = false, $tables = [], $inputs = [], $formSet = true, $tablesFormData = [])
    {
        return parent::saveInputs($datas, $where, $inId, $tables, $inputs, $formSet, $tablesFormData); // TODO: Change the autogenerated stub
    }

    public static function getNextSetup()
    {
        return parent::getNextSetup(); // TODO: Change the autogenerated stub
    }

    public static function inputsSetup()
    {
        return parent::inputsSetup(); // TODO: Change the autogenerated stub
    }

    public static function setupDesc()
    {
        //return parent::setupDesc(); // TODO: Change the autogenerated stub
        return [
            '1'  => '基本信息',
            '10' => '字段效验',
        ];
    }

    public static function batchSaveFields()
    {
    }

}